<?php

namespace App\Http\Controllers\user;

use App\Dao\UsersDao;
use App\Exceptions\SysExceptions;
use App\Services\Code;
use App\Services\OssServices;
use App\Services\WechatServices;

class UsersController extends BaseController
{


    public function wechatLogin(): array
    {
        $wechatInfo = WechatServices::wx_publick_login(request('code'));
        UsersDao::updateHeadNIckname($wechatInfo,getUserToken());
        return Code::message('',200);
    }

    /**
     * @throws SysExceptions
     */
    public function login(): array
    {
        $params = request()->post();
        return Code::message('',200,UsersDao::login($params));
    }


    public function userInfo(): array
    {
        return Code::message('',200,UsersDao::getUserInfo([
            'user_id'=>getUserToken()
        ]));
    }

    public function file_up(): array
    {
        $filePath = $_FILES["file"]["tmp_name"];
        $ext = substr($_FILES['file']['name'],strrpos($_FILES['file']['name'],'.')+1); // 上传文件后缀
        $object=date('YmdHis').'-'.time().'.'.$ext;
        $oss = new OssServices();
        $url = $oss->upload($object,$filePath);
        return Code::message('',200,[
            'url'=>$url
        ]);

    }


    /**
     * @throws SysExceptions
     */
    public function update_user_data(): array
    {
        $params = request()->all();
        $params['user_id'] = getUserToken();
        UsersDao::setUserData($params);
        return Code::message();
    }


    /**
     * @throws SysExceptions
     */
    public function set_pay_pass(): array
    {
        if(isPost()){
            $params = request()->all();
            $params['user_id'] = getUserToken();
            UsersDao::setPayPass($params);
            return  Code::message('',200);
        }
        return Code::message('',200);
    }


    public function se_login_pass(): array
    {
        $params = request()->all();
        UsersDao::set_login_pass($params);
        return Code::message();
    }


    public function moneyToUsers(): array
    {
        $params=  request()->all();
        $params['user_id'] = getUserToken();
        UsersDao::moneysToUsers($params);
        return Code::message('',200);
    }
}
